package com.bbcare.department.plat.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.ToolUtils;
import com.bbcare.department.plat.entity.Contact;
import com.bbcare.department.plat.service.IDepartmentContactService;

@Controller
@RequestMapping("/service/department")
public class DepartmentContactController {
	Logger logger = Logger.getLogger(DepartmentContactController.class);
	@Autowired
	private IDepartmentContactService departmentContactService;

	/*@ResponseBody
	@RequestMapping("/contact/detail")
	public ResultModel<Contact> getContactDetail(@RequestBody String jsonStr) throws Exception{
		logger.info("in getContactDetail----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        String id = jsonEsb.getString("id");
        if(StringUtils.isEmpty(id)){
        	logger.debug("out getDepartmentBasic----arg== null");
        	return new ResultModel<Contact>(null);
        }
        Contact contact = departmentContactService.getContactById(id);
		logger.info("out getContactDetail----arg==");
		return new ResultModel<Contact>(contact);
	}
	*/
	@ResponseBody
	@RequestMapping("/contact/modify")
	public ResultModel<Map<String,String>> modifyContact(HttpServletRequest request,@RequestBody String jsonStr) throws Exception{
		logger.info("in contactModify----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}

        String contacts= requestParam.getString("contactModify");
        List<Contact> contactList = JSON.parseArray(contacts, Contact.class);
        /*
        Object account = session.getAttribute("ACCOUNT");
        String createAuthor = 从session取用户ID
        departmentBasic.setCreateAuthor(createAuthor);*/

        // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String createAuthor = (String) sessionMap.get("authorId");
        String deptId = (String) sessionMap.get("rootGroupId");// 租户id

        if(CollectionUtils.isEmpty(contactList)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败,地址信息不能为空");
        }

        for(int i = 0 ;i<contactList.size();i++){
        	contactList.get(i).setDeptId(deptId);
			if(StringUtils.isEmpty(contactList.get(i).getId())){
				contactList.get(i).setCreateAuthor(createAuthor);
			}else{
				contactList.get(i).setUpdateAuthor(createAuthor);
			}
		}

        departmentContactService.contactMoidify(contactList);
		logger.info("out contactModify----arg==");
//		Map<String,String> rspBody = new HashMap<String,String>();
//		rspBody.put("deptId",deptId);
		return new ResultModel<Map<String,String>>(null);
	}

	@ResponseBody
	@RequestMapping("/contact/detail")
	public ResultModel<List<Contact>> getContacts(HttpServletRequest request,@RequestBody String jsonStr) throws Exception{
		logger.info("in getContacts----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}
		 // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String deptId = (String) sessionMap.get("rootGroupId");// 租户id

        List<Contact> list = departmentContactService.getContactsByDeptId(deptId);
		logger.info("out getContacts----arg==" );
		return new ResultModel<List<Contact>>(list);
	}

	@ResponseBody
	@RequestMapping("/contact/remove")
	public ResultModel<Contact> deleteContact(@RequestBody String jsonStr) throws Exception{
		logger.info("in deleteContact----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}
        String ids= requestParam.getString("contactIds");

        List<String> list = JSON.parseArray(ids, String.class);
        if(CollectionUtils.isEmpty(list)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，要删除的地址ID不能为空");
        }

		departmentContactService.deleteContactByIds(list);
		logger.info("out deleteContact----arg==");
		return new ResultModel<Contact>(null);
	}
}
